using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public abstract class BuffBase : MonoBehaviour
{
    protected bool isCountDown;
    protected int buffCountDown;
    protected bool isDone = false;
    protected AbstractCharacter character;
    protected AbstractEnemy enemy;
    protected GameObject BuffItem;
    protected TextMesh BuffText;

    protected virtual void OnEnable()
    {
        if (TryGetComponent<AbstractEnemy>(out _))
        {
            enemy = this.transform.GetComponent<AbstractEnemy>();
            enemy.AddBuffToList(this);
            FightManager.Instance.OnTurnToPlayer += CountDown;
        }
    }

    protected virtual void CountDown()
    {
        if (isCountDown && enemy != null)
        {
            if (buffCountDown > 1)
            {
                buffCountDown--;
                UpdateCountText();
            }
            else if(buffCountDown == 1)
            {
                //Destroy(transform.GetComponent<BuffBase>());
                isDone = true;
                FightManager.Instance.OnTurnToPlayer -= CountDown;
                enemy.RemoveBuffFromList(this);
            }
        }
    }

    public virtual void CountUp(int val)
    {
        if (isCountDown)
        {
            buffCountDown += val;
            UpdateCountText();
        }
    }

    public virtual void SetStartCount(int val)
    {
        buffCountDown = val;
        UpdateCountText();
    }

    protected virtual void UpdateCountText()
    {
        BuffText.text = buffCountDown.ToString();
    }
}
